package com.hotel.front;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.Util;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hotel.BL.POSPaymentLogic;
import com.hotel.BL.POSgenerateOrderLogic;

import data.POSCalTax;
import data.POSGuestTable;
import data.POSOrder;
import data.POSorderItem;
import data.passingData.POSEditOrderItem;

@SuppressWarnings("serial")
public class POSPaymentService extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		try {
			String func = req.getParameter("func");
			if (func.equals("SetPOSFolio")) {
				SetPOSFolio(req, resp);
			}else if(func.equals("EditPaymentOrderItem"))
				EditPaymentOrderItem(req,resp);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void SetPOSFolio(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		POSPaymentLogic posPaymentLogic = new POSPaymentLogic(req,resp);
		long guestTableId = Long.parseLong(req.getParameter("guestTableId"));
		POSGuestTable guestTable = posPaymentLogic.setFolioToGuestTable(guestTableId);
		
		Gson gson = new Gson();
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(guestTable));
	}

	private void EditPaymentOrderItem(HttpServletRequest req, HttpServletResponse resp) throws IOException, EntityNotFoundException {
		POSPaymentLogic posPaymentLogic = new POSPaymentLogic(req,resp);
		POSgenerateOrderLogic posGenOrderLogic = new POSgenerateOrderLogic(req,resp);
		Gson gson = new Gson();
		
		String fromOrder = req.getParameter("fromOrder");
		String itemsString = req.getParameter("items");
		long gResId = Long.parseLong(req.getParameter("gResId"));
		Type listType = new TypeToken<List<POSEditOrderItem>>() {}.getType();
		List<POSEditOrderItem> itemList = gson.fromJson(itemsString, listType);
		List<POSOrder> orderList = new ArrayList<POSOrder>();
		String[] orderIdList = fromOrder.split(",");
		for(String orderId : orderIdList){
			POSOrder order = posGenOrderLogic.GetPOSOrder(Long.parseLong(orderId));
			orderList.add(order);
		}
		
		posPaymentLogic.EditPaymentOrderItem(orderList,itemList);
		
		POSGuestTable gt = posGenOrderLogic.GetPOSGuestTable(gResId);
		List<HashMap<String,Object>> listOfOrder = new ArrayList<HashMap<String,Object>>();
		double money =0;
		if(gt.getPOSorderList()!=null){
			for(POSOrder d:gt.getPOSorderList()){
				if(!d.isStatus()) continue;
				for(POSorderItem i:d.getReloadItem()){
					money += (i.getPrice() * i.getAmount()) - i.getDiscount();
					List<POSCalTax> calTax = i.getTax();
					if(calTax!=null)
						for(POSCalTax tax : calTax)
							money += tax.getTaxValue() * i.getAmount();
				}
				HashMap<String,Object> hash = Util.toHashmapformfield(d);
				hash.remove("hotelId");
				hash.remove("itemString");
				hash.put("items", hash.get("item"));
				hash.remove("item");
				listOfOrder.add(hash);
			}
		}
		HashMap<String,Object> hashResult = new HashMap<String,Object>();
		hashResult.put("m", money);
		hashResult.put("o",listOfOrder);
		
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(hashResult));
	}
}